//
//  StartCustomMadeViewController.m
//  didacar
//
//  Created by imac on 15/12/3.
//  Copyright © 2015年 dzmmac. All rights reserved.
//

#import "StartCustomMadeViewController.h"
#import "BuyTicketHeaderView.h"
#import "SelectLocationViewController.h"
#import "DatePickerViewController.h"
#import "UIViewController+WtPrestent.h"
#import "CustomViewModel.h"
#import "RoutePlanningViewController.h"
#import "InitiateCustomModel.h"
#import "RequestCustomModel.h"
#import "UserInfoManager.h"
#import "UserInfoModel.h"

@interface StartCustomMadeViewController () <SelectLocationDelegate>

@property (strong, nonatomic) BuyTicketHeaderView *buyTicketHeaderView;
- (IBAction)startBtnAction:(UIButton *)sender;

// 按钮的标记
@property (assign, nonatomic) NSInteger btnTag;

@property (strong, nonatomic) PoisModel *startPoisModel;
@property (strong, nonatomic) PoisModel *endPoisModel;

@property (strong, nonatomic) CustomViewModel *customViewModel;

@end

@implementation StartCustomMadeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setLeftBtnItem];
    self.buyTicketHeaderView = [BuyTicketHeaderView  getInstance];
    self.buyTicketHeaderView.alertStyle = 3;
    self.buyTicketHeaderView.timeTextField.text = @"08:00";
    [self.view addSubview:self.buyTicketHeaderView];
    WEAKSELF
    self.buyTicketHeaderView.BtnBlock = ^void(NSInteger btnTag){
        weakSelf.btnTag = btnTag;
        
        // 出发地或者目的地
        if (btnTag == 1 || btnTag == 2) {
            
            [weakSelf presentSelectLocation];
        }
        // 切换地点
        else if (btnTag == 3){
            [weakSelf changeLocation];
        }
        // 选择时间
        else if (btnTag == 4){
            [weakSelf timeActionWithCurrentText];
            
        }
    };
    
    self.customViewModel = [[CustomViewModel alloc] init];
}

-(void)changeLocation{
    
    PoisModel *tempPoiModel = [[PoisModel alloc ] init];
    tempPoiModel.latitude = self.startPoisModel.latitude;
    tempPoiModel.city     = self.startPoisModel.city;
    tempPoiModel.name     = self.startPoisModel.name;
    tempPoiModel.address  = self.startPoisModel.address;
    tempPoiModel.longitude= self.startPoisModel.longitude;
    self.startPoisModel = self.endPoisModel;
    self.endPoisModel   = tempPoiModel;
}

/**
 *  选择时间
 *
 *  @param text 之前的时间
 */
-(void)timeActionWithCurrentText
{
    DatePickerViewController *vcDate = [self.storyboard instantiateViewControllerWithIdentifier:@"DatePickerViewController"];
    
    vcDate.requiredSelectedTime = self.buyTicketHeaderView.timeTextField.text;
    
    WEAKSELF
    vcDate.cancelBlock = ^(){
        [weakSelf dismissWtViewControllerAnimated:YES];
    };
    vcDate.ensureBlock = ^(NSMutableArray *msg){
        [weakSelf dismissWtViewControllerAnimated:YES];
        weakSelf.buyTicketHeaderView.timeTextField.text = [NSString stringWithFormat:@"%@:%@",[msg firstObject],[msg lastObject]];
    };
    
    [self presentWtViewController:vcDate animated:YES];
}

-(void)presentSelectLocation{
    UINavigationController *cityNavigation = (UINavigationController *)[BaseViewController getInitialViewControllerFromSbName:@"SelectLocation"];
    SelectLocationViewController *selecetdLocation = cityNavigation.viewControllers[0];
    selecetdLocation.delegate = self;
    [self presentViewController:cityNavigation animated:YES completion:^{
        
    }];
}

-(void)selectedPoiModel:(PoisModel *)poisModel{
    if (self.btnTag == 1) {
        self.startPoisModel = poisModel;
        self.buyTicketHeaderView.startTextField.text = poisModel.name;
    }else{
        self.endPoisModel = poisModel;
        self.buyTicketHeaderView.endTextField.text = poisModel.name;
    }
}

#pragma mark -------发起定制按钮点击---------------
- (IBAction)startBtnAction:(UIButton *)sender {
    if ([self.buyTicketHeaderView validata]){
        
        if (USERINFOManager.getUserInfo.loginStatus == YES) {
             [self startOrder];
        }else{
            [self jumperLogin];
        }
    }
}

#pragma mark -------发起定制请求---------------
-(void)startOrder{
    self.customViewModel.requestCustomModel.StartStation = self.startPoisModel.address;
    self.customViewModel.requestCustomModel.EndStation   = self.endPoisModel.address;
    self.customViewModel.requestCustomModel.StartLng = [NSDecimalNumber decimalNumberWithString:@(self.startPoisModel.longitude).stringValue];
    self.customViewModel.requestCustomModel.StartLat = [NSDecimalNumber decimalNumberWithString:@(self.startPoisModel.latitude).stringValue];
    self.customViewModel.requestCustomModel.EndLng = [NSDecimalNumber decimalNumberWithString:@(self.endPoisModel.longitude).stringValue];
    self.customViewModel.requestCustomModel.EndLat = [NSDecimalNumber decimalNumberWithString:@(self.endPoisModel.latitude).stringValue];
    self.customViewModel.requestCustomModel.SendTime = self.buyTicketHeaderView.timeTextField.text;
    self.customViewModel.requestCustomModel.MemberId = USERINFOManager.getUserInfo.MemberId;
    
    [self.customViewModel initiateCustom:^(NSInteger resultType, id data, NSString *errorMsg) {
        if (resultType == 1) {
            // 定制成功
            [HudManager showSuccess:@"我们已经收到您的定制信息！"];
            RoutePlanningViewController *routePlanningViewController = (RoutePlanningViewController *)[RoutePlanningViewController getViewControllerFromSbName:@"RoutePlanning" viewControllerName:@"RoutePlanningViewController"];
            routePlanningViewController.routePlanningStatus = RoutePlanningWaite;
            [self.navigationController pushViewController:routePlanningViewController animated:YES];
        }else{
            [HudManager showFailure:errorMsg];
        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
